<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>共{{total}}条记录</span>
        <el-button
          class="My_button"
          style="float: right; padding: 3px 0"
          type="primary"
          icon="el-icon-plus"
          round
          @click="modifyFn()"
        >添加面经</el-button>
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="180"></el-table-column>
        <el-table-column prop="creator" label="作者" width="180"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"></el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column prop="createdAt" label="更新时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <i class="el-icon-view" @click="LookDetail(scope.row.id)"></i>
            <i class="el-icon-edit-outline" @click="modifyFn(scope.row.id)"></i>
            <i class="el-icon-folder-delete" @click="deleteAritcal(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <artical-detail ref="detail" />
    <modify-aritical ref="modify" @updata="getArticalListFn" />
  </div>
</template>

<script>
import { articalListApi, deleteArticalApi } from '@/api/list'
import ArticalDetail from './components/articalDetail.vue'
import ModifyAritical from './components/ModifyAritical.vue'
export default {
  name: 'ArticalList',
  props: {},
  components: {
    ArticalDetail,
    ModifyAritical
  },
  data () {
    return {
      list: [],
      total: 0,
      current: 1, // 当前页
      pageSize: 10
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getArticalListFn()
  },
  mounted () {},
  methods: {
    async  getArticalListFn () {
      const { data: { total, rows } } = await articalListApi({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = rows
      this.total = total
    },
    handleSizeChange (val) {
      this.pageSize = val
      console.log(this.pageSize)
      this.getArticalListFn()
    },
    handleCurrentChange (val) {
      this.current = val
      this.getArticalListFn()
    },
    async  deleteAritcal (id) {
      if (this.list.length <= 1 && this.current > 1) {
        this.current -= 1
      }
      await deleteArticalApi({
        id
      })
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getArticalListFn()
    },
    LookDetail (id) {
      this.$refs.detail.open(id)
    },
    modifyFn (id) {
      this.$refs.modify.open(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin-top: 20px;
  .My_button {
    width: 100px;
    height: 30px;
  }
  i {
    margin-left: 20px;
    font-size: 20px;
  }
  .el-pagination {
    text-align: center;
    margin-top: 20px;
  }
}
</style>
